<?php
    // +----------------------------------------------------------------------
    // | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ]
    // +----------------------------------------------------------------------
    // | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
    // +----------------------------------------------------------------------
    // | SiteUrl: http://www.luckyhhy.cn
    // +----------------------------------------------------------------------
    // | Author: LuckyHhy <jackhhy520@qq.com>
    // +----------------------------------------------------------------------
    // | Date: 2020/1/3-15:49
    // +----------------------------------------------------------------------
    // | Description:  
    // +----------------------------------------------------------------------


    namespace app\admin\model;


    use app\admin\imple\Comm;
    use app\admin\service\MenuService;
    use app\common\model\BaseModel;
    use Jrk\Trees;
    use think\db\Where;
    use think\Exception;


    class Menus extends BaseModel implements Comm
    {

        protected $name ="menu";

        protected function initialize()
        {
            parent::initialize(); // TODO: Change the autogenerated stub

        }


        /**
         * @param array $param
         * @param string $order
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/1/3
         * @name: getAdminMenuPageData
         * @describe:后台菜单列表数据获取
         */
        public function getAdminPageData($param=[],$order='listorder desc'){

            $where = [];
            if(!empty($param)) {
                //搜索条件
                if(!empty($param['title'])) {
                    $where['title'] = ['like', "%".$param['title']."%"];
                    //$where[] = ['title','like', "%".$param['title']."%"];
                }
            }
            try{
                $data = self::where(new Where($where))->order($order)->select()->toArray();
                $count =self::where(new Where($where))->count("id");
                //dump(self::getLastSql());
                //dump($where);
               //die;
                if (!empty($data)){
                    $data=Trees::sortListTier($data);
                }

                $this->ajaxResult($data,0,'',$count);

            }catch (Exception $exception){

                $this->ajaxResult('',100,$exception->getMessage());
            }

        }



        /**
         * @param $data
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/1/9
         * @name: addAndEdit
         * @describe:添加或者编辑
         */
        public function addAndEdit($data){
            return parent::doAll($data);
        }



        /**
         * @param $id
         * @return mixed|void
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/1/9
         * @name: del
         * @describe:删除
         */
        public function del($id)
        {
            // TODO: Implement delete() method.
            $res = $this->findChild($id);
            if($res!==true) {
                $this->error("当前菜单下有子集，不能删除");
            }
            try {
                $del = $this->destroy(['id' => ['in', $id]]);
                $del?$this->success("删除成功"):$this->error("删除失败");
            } catch (Exception $exception) {
                 $this->error($exception->getMessage());
            }

        }


        /**
         * @param $id
         * @return bool
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/1/9
         * @name: findChild
         * @describe:查询是否有子集
         */
        private function findChild($id)
        {
            $res = Trees::getChildrenPid(MenuService::menuList(), $id);
            if(empty($res)) {
                return true;
            }
            else {
                return false;
            }
        }


    }